package com.study.servlet.session.prac02;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.study.servlet.session.prac02.domain.Book;

/**
* @Name: ShowCart
* @Description: Session应用：实现购物车
* @Author: 李超	lc_jack@foxmail.com
* @CreateDate: 2017-3-3 下午3:52:02 
* @Version: V1.0
 */
public class ShowCart extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setContentType("text/html;charset=UTF-8") ;
		PrintWriter out = response.getWriter() ;
		//显示所有购买的书籍
		HttpSession session = request.getSession() ;
		List<Book> list = (List<Book>) session.getAttribute("CART") ;
		if(list != null && list.size() > 0) {
			for (Book book : list) {
				out.write(book.getName() + "<br/>") ;
			}
		} else {
			out.write("购物车为空！！！") ;
		}
		//重写Url
		String showAllBooksUrl = request.getContextPath() + "/servlet/ShowAllBooks" ;
		String url = response.encodeURL(showAllBooksUrl) ;
		out.write("<a href="+ url +">返回</a>") ;
		out.close() ;
		out.close() ;
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doGet(request, response);
	}

}
